<template>
  <div class="c-f-d-functions">
    <div class="c-f-d-functions-group" v-for="(item, index) in functions" :key="index">
      <div class="c-f-d-functions-group-title"><i class="el-icon-menu"></i>{{ item.title }}</div>
      <el-checkbox-group v-if="item.funcs && item.funcs.length > 0" v-model="form.functions" size="mini" @change="updateFunctions()">
        <el-checkbox v-for="(func, index) in item.funcs" :key="index" :label="func.code" border>{{ func.name }}
        </el-checkbox>
      </el-checkbox-group>
      <div class="c-f-d-functions-group-empty" v-else>暂无{{ item.title }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: "FlowFunctions",
  props: {
    businessObject: Object,
    type: String,
    id: String,
    // 节点功能
    functions: {
      type: Array
    },
  },
  computed: {
    default_functions() {
      return this.functions ||
          [
            {
              title: "系统功能",
              funcs: []
            },
            {
              title: "自定义功能",
              funcs: []
            }
          ];
    }
  },
  data() {
    return {
      form: {
        functions: [],
      }
    };
  },
  watch: {
    businessObject: {
      immediate: true,
      handler: function (val) {
        if (val) {
          this.$nextTick(() => this.resetBaseInfo());
        }
      }
    }
  },
  methods: {
    resetBaseInfo() {
      this.bpmnElement = window?.bpmnInstances?.bpmnElement;
      let businessObject = JSON.parse(JSON.stringify(this.bpmnElement.businessObject));
      let functions = businessObject.functions || "";
      this.form = {functions: functions ? functions.split(",") : []};
    },
    updateFunctions() {
      window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {functions: this.form.functions.join(",")});
    },
  },
  beforeDestroy() {
    this.bpmnElement = null;
  }
}
</script>

<style scoped lang="scss">

.c-f-d-functions {

  .el-checkbox-group {
    margin-left: 15px;

    .el-checkbox {
      margin-right: 10px;
      margin-top: 5px;
    }

    .el-checkbox.is-bordered + .el-checkbox.is-bordered {
      margin-left: 0;
    }
  }

  &-group + &-group {
    margin-top: 10px;
  }

  &-group {
    width: 100%;

    &-title {
      margin-left: 5px;
    }

    &-empty {
      text-align: center;
    }

    span {
      width: 200px;
      text-align: left;
      font-size: 14px;
    }

    i {
      margin-right: 8px;
    }
  }
}

</style>
